Java basic byte[] simple example of interconversion with various data types


Java basic byte[] simple example of interconversion with various data types

Here are examples of interconversion of byte[] types to long,int,double,float, short,cahr, object,string types,

During the development of socket, it is usually necessary to convert 1 specific value (which may be of various Java types) to byte[]. For this reason, I summarized the following example and posted it for frequent review:

public class TestCase {

  /**
   * short Conversion to a byte array .
   */
  public static byte[] shortToByte(short number) {
    int temp = number;
    byte[] b = new byte[2];
    for (int i = 0; i < b.length; i++) {
      b[i] = new Integer(temp & 0xff).byteValue();//  Save the lowest bit in the lowest bit
      temp = temp >> 8;//  Move to the right 8 position
    }
    return b;
  }

  /**
   *  Byte array to short The transformation of the .
   */
  public static short byteToShort(byte[] b) {
    short s = 0;
    short s0 = (short) (b[0] & 0xff);//  Its lowest
    short s1 = (short) (b[1] & 0xff);
    s1 <<= 8;
    s = (short) (s0 | s1);
    return s;
  }


  /**
   * int Conversion to a byte array .
   */
  public static byte[] intToByte(int number) {
    int temp = number;
    byte[] b = new byte[4];
    for (int i = 0; i < b.length; i++) {
      b[i] = new Integer(temp & 0xff).byteValue();//  Save the lowest bit in the lowest bit
      temp = temp >> 8;//  Move to the right 8 position
    }
    return b;
  }

  /**
   *  Byte array to int The transformation of the .
   */
  public static int byteToInt(byte[] b) {
    int s = 0;
    int s0 = b[0] & 0xff;//  Its lowest
    int s1 = b[1] & 0xff;
    int s2 = b[2] & 0xff;
    int s3 = b[3] & 0xff;
    s3 <<= 24;
    s2 <<= 16;
    s1 <<= 8;
    s = s0 | s1 | s2 | s3;
    return s;
  }


  /**
   * long Type into byte An array of
   */
  public static byte[] longToByte(long number) {
    long temp = number;
    byte[] b = new byte[8];
    for (int i = 0; i < b.length; i++) {
      b[i] = new Long(temp & 0xff).byteValue();//  Save the lowest bit in the lowest bit  temp = temp
                            // >> 8;//  Move to the right 8 position
    }
    return b;
  }

  /**
   *  Byte array to long The transformation of the .
   */
  public static long byteToLong(byte[] b) {
    long s = 0;
    long s0 = b[0] & 0xff;//  Its lowest
    long s1 = b[1] & 0xff;
    long s2 = b[2] & 0xff;
    long s3 = b[3] & 0xff;
    long s4 = b[4] & 0xff;//  Its lowest
    long s5 = b[5] & 0xff;
    long s6 = b[6] & 0xff;
    long s7 = b[7] & 0xff;

    // s0 The same
    s1 <<= 8;
    s2 <<= 16;
    s3 <<= 24;
    s4 <<= 8 * 4;
    s5 <<= 8 * 5;
    s6 <<= 8 * 6;
    s7 <<= 8 * 7;
    s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
    return s;
  }

  /**
   * double Conversion to a byte array .
   */
  public static byte[] doubleToByte(double num) {
    byte[] b = new byte[8];
    long l = Double.doubleToLongBits(num);
    for (int i = 0; i < 8; i++) {
      b[i] = new Long(l).byteValue();
      l = l >> 8;
    }
    return b;
  }

  /**
   *  Byte array to double The transformation of the .
   */
  public static double getDouble(byte[] b) {
    long m;
    m = b[0];
    m &= 0xff;
    m |= ((long) b[1] << 8);
    m &= 0xffff;
    m |= ((long) b[2] << 16);
    m &= 0xffffff;
    m |= ((long) b[3] << 24);
    m &= 0xffffffffl;
    m |= ((long) b[4] << 32);
    m &= 0xffffffffffl;
    m |= ((long) b[5] << 40);
    m &= 0xffffffffffffl;
    m |= ((long) b[6] << 48);
    m &= 0xffffffffffffffl;
    m |= ((long) b[7] << 56);
    return Double.longBitsToDouble(m);
  }


  /**
   * float Conversion to a byte array .
   */
  public static void floatToByte(float x) {
    // Use first  Float.floatToIntBits(f) Converted to int
  }

  /**
   *  Byte array to float The transformation of the .
   */
  public static float getFloat(byte[] b) {
    // 4 bytes
    int accum = 0;
    for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) {
        accum |= (b[shiftBy] & 0xff) << shiftBy * 8;
    }
    return Float.intBitsToFloat(accum);
  }

   /**
   * char Conversion to a byte array .
   */
   public static byte[] charToByte(char c){
    byte[] b = new byte[2];
    b[0] = (byte) ((c & 0xFF00) >> 8);
    b[1] = (byte) (c & 0xFF);
    return b;
   }

   /**
   *  Byte array to char The transformation of the .
   */
   public static char byteToChar(byte[] b){
    char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
    return c;
   }

  /**
   * string Conversion to a byte array .
   */
  public static byte[] stringToByte(String str) throws UnsupportedEncodingException{
    return str.getBytes("GBK");
  }

  /**
   *  Byte array to String The transformation of the .
   */
  public static String bytesToString(byte[] str) {
    String keyword = null;
    try {
      keyword = new String(str,"GBK");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return keyword;
  }


  /**
   * object Conversion to a byte array
   */
  @Test
  public void testObject2ByteArray() throws IOException,
      ClassNotFoundException {
    // Object obj = "";
    Integer[] obj = { 1, 3, 4 };

    // // object to bytearray
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(bo);
    oo.writeObject(obj);
    byte[] bytes = bo.toByteArray();
    bo.close();
    oo.close();
    System.out.println(Arrays.toString(bytes));

    Integer[] intArr = (Integer[]) testByteArray2Object(bytes);
    System.out.println(Arrays.asList(intArr));


    byte[] b2 = intToByte(123);
    System.out.println(Arrays.toString(b2));

    int a = byteToInt(b2);
    System.out.println(a);

  }

  /**
   *  Byte array to object The transformation of the .
   */
  private Object testByteArray2Object(byte[] bytes) throws IOException,
      ClassNotFoundException {
    // byte[] bytes = null;
    Object obj;
    // bytearray to object
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    ObjectInputStream oi = new ObjectInputStream(bi);
    obj = oi.readObject();
    bi.close();
    oi.close();
    System.out.println(obj);
    return obj;
  }

}

Thank you for reading, I hope to help you, thank you for your support of this site!